import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/views/Layout.vue'
import Home from '@/views/Home.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: Layout,
      children: [
        {
          path: '',
          name: 'home',
          component: Home,
        },
        {
          path: 'sub-app1/:pathMatch(.*)*',
          name: 'SubApp1',
          component: () => import('../views/SubApp.vue'),
        },
        {
          path: 'sub-app2/:pathMatch(.*)*',
          name: 'SubApp2',
          component: () => import('../views/SubApp.vue'),
        },
      ],
    },
  ],
})

// 路由守卫，处理子应用路由
router.beforeEach((to, from, next) => {
  // 记录当前激活的子应用
  if (to.path.startsWith('/sub-app1') || to.path.startsWith('/sub-app2')) {
    const appName = to.path.split('/')[1]
    sessionStorage.setItem('activeSubApp', appName)
  }
  next()
})

export default router
